//
//  ViewController.m
//  Homework_22
//
//  Created by 徐昊 on 16/8/12.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "Data.h"
#import "CustomCollectionViewCell.h"
#import "List.h"
#import "MV.h"
#import "MV_show_play.h"
#import "Contents.h"
#import "Data.h"

@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) Data *data;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self handleData];
    _layout.minimumLineSpacing = 5;
    _layout.minimumInteritemSpacing = 5;
}
- (void)handleData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *requestURLString = @"http://i.y.qq.com/v8/fcg-bin/getmv_by_tag?cmd=mv_shows_new&g_tk=5381&uin=0&format=json&inCharset=utf-8&outCharset=utf-8&notice=0&platform=h5&needNewCode=1&_=1471048353027";
    [manager GET:requestURLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError *error = nil;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        _data = [Data modelWithDic:dataDic];
        [_collectionView reloadData];
        NSLog(@"12938712123123%@", _data.contents.list[0].mvList[1].singer_name);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败");
        
    }];
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return CGSizeMake(self.view.bounds.size.width - 10, 200);
    } else {
        return CGSizeMake((self.view.bounds.size.width - 15) / 2, 100);
    }
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _data.contents.list[section].mvList.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _data.contents.list.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.mvName.text = _data.contents.list[indexPath.section].mvList[indexPath.row].singer_name;
    NSLog(@"%@", cell.mvName.text);
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
